package com.xzhuang.task.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xzhuang.task.util.QuartzUtil;

public class LoginFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
		// TODO Auto-generated method stub

		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();

		// 获得用户请求的URI
		String path = request.getRequestURI();
		String contextPath = request.getContextPath();
		String url = path.substring(contextPath.length());
		System.out.println("------------------->: " + url);

		String[] extName = { "jpg", "js", "css", "png", "gif" };
		List fiterList = new ArrayList();
		// fiterList.add("http://localhost:8080/task/login");
		// fiterList.add("http://localhost:8080/task/");
		// fiterList.add("http://localhost:8080/task/login.jsp");
		fiterList.add("/login");
		fiterList.add("/");
		fiterList.add("/login.jsp");
		fiterList.add("/monitor/add");
		String user = (String) session.getAttribute(QuartzUtil.USERINFO);
		// String curPath = request.getRequestURL().toString();
		// System.out.println(getUrlLastExtName(curPath) +
		// "=====================>> curpath:" + curPath);

		// if (curPath.indexOf("GPS/User/Index") >= 0) { return true; }
		String bl = getUrlLastExtName(url);
		if (!fiterList.contains(url)) {
			if (!Arrays.asList(extName).contains(bl)) {
				if (null == user) {
					// 未登录
					response.setCharacterEncoding("UTF-8");
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();

					StringBuilder builder = new StringBuilder();
					builder.append("<script type=\"text/javascript\" charset=\"UTF-8\">");
					builder.append("alert(\"页面过期，请先登录!\");");
					builder.append("window.location.href=\"login.jsp\";");
					builder.append("</script>");
					// builder.append(Constants.basePath);
					// builder.append("/background/index.html\";</script>");
					out.print(builder.toString());
					out.close();
					return;
				}
			}
		}

		filterChain.doFilter(servletRequest, servletResponse);

	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}

	public String getUrlLastExtName(String url) {
		if (url != null && url.length() > 0) {
			if (url.indexOf(".") != -1) {
				return url.substring(url.lastIndexOf(".") + 1, url.length());
			}
		} else {
			return url;
		}
		return null;
	}

}
